package com.cndw.kungfu.model.scene;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cndw.kungfu.config.ConstantsTask;
import com.cndw.kungfu.dao.NpcDao;
import com.cndw.kungfu.dao.SceneDao;
import com.cndw.kungfu.domain.GoodsMode;
import com.cndw.kungfu.domain.Monster;
import com.cndw.kungfu.domain.Npc;
import com.cndw.kungfu.domain.Pendant;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.domain.Scene;
import com.cndw.kungfu.model.Context;

/**
 * 场景操作
 * 
 * @author fantadust
 * 
 */
@Component
public class SceneManager {
	@Autowired
	private SceneDao dao;
	@Autowired
	private NpcDao npcDao;

	public SearchResult searchObject(int action, int targetId,long targetPlayerId) {
		int fianlSceneId = 0;
		int monsterId = 0;
		String finalName = null;
		int x =0 , y = 0,subLine =0;

		if (action == ConstantsTask.actionCollect || action == ConstantsTask.actionGather) {// 1
			GoodsMode goodsMode = Context.getGoodsServer().getModeById(targetId);
			if (goodsMode == null) {
				return null;
			}
			if (action == ConstantsTask.actionCollect) {
				monsterId = goodsMode.getFallByMonsterId();
				if (monsterId == 0) {
					return null;
				}
				finalName = goodsMode.getName();
			} else {
				if (goodsMode.getGatherScene() != null) {
					fianlSceneId = goodsMode.getGatherScene()[0];
					x = goodsMode.getGatherScene()[1];
					y = goodsMode.getGatherScene()[2];					
				}
				
				finalName = goodsMode.getName();
			}
		} else if (action == ConstantsTask.actionKill) {// 猎杀
			monsterId = targetId;
		} else if (action == ConstantsTask.actionNpc) {// 寻npc
			Npc npc = Context.getSceneServer().getNpcById(targetId);
			if (npc == null) {
				return null;
			}
			fianlSceneId = npc.getSceneId();
			finalName = npc.getName();
			x = npc.getX();
			y = npc.getY();
			
		} else if (action == ConstantsTask.actionPlayer) {
			Player player = Context.getPlayerServer().getByPlayerId(targetPlayerId);
			if (player == null) {
				return null;
			}
			fianlSceneId = player.getSceneId();
			finalName = player.getPlayerName();
			x = player.getX();
			y = player.getY();	
			subLine = player.getSubLine();
		}else if (action == ConstantsTask.actionScene) {
			fianlSceneId = targetId;
		}
		if (monsterId > 0) {
			Monster monster = Context.getCacheCenter().getMonsterById(monsterId);
			if (monster == null) {
				return null;
			}
			if (monster.getInScend() != null) {
				fianlSceneId = monster.getInScend()[0];
				x = monster.getInScend()[1];
				y = monster.getInScend()[2];
			}
			
			if (finalName == null) {
				finalName = monster.getName();
			}
		}
		Scene scene = Context.getSceneServer().getSceneById(fianlSceneId);
		if (scene == null) {
			return null;
		}
		if (finalName == null) {
			finalName = scene.getName();
		}
		int rootId = scene.getRootId() == 0 ? fianlSceneId : scene.getRootId();
		SearchResult result = new SearchResult();
		result.setFianlSceneId(fianlSceneId);
		result.setFinalSceneType(scene.getType());
		result.setFinalSceneRootId(rootId);
		result.setFinalName(finalName);
		result.setMonsterId(monsterId);
		result.setSearchType(action);	
		result.setX(x);
		result.setY(y);
		result.setSubLine(subLine);

		return result;
	}

	public Collection<Scene> getAllScenes() {
		return Context.getCacheCenter().getAllScenes();
	}

	/**
	 * 获取一组的npc对象
	 * 
	 * @param npcIds
	 * @return
	 */
	public List<Npc> getNpcByIds(long[] npcIds) {
		if (npcIds.length <= 0)
			return null;
		List<Npc> npcs = new ArrayList<Npc>();
		for (int i = 0; i < npcIds.length; i++) {
			int npcId = (int) npcIds[i];
			Npc e = Context.getSceneServer().getNpcById(npcId);
			npcs.add(e);
		}
		return npcs;
	}

	/**
	 * 一次获取全部场景
	 */
	public List<Scene> getAllScenesFromDb() {
		try {
			return dao.getAllScenes();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<Npc> getAllNpcsFromDb() {
		try {
			return npcDao.getAllNpcsFromDb();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public Map<Integer, Pendant> getpPendantsFromDb() {
		try {
			return dao.getAllPendants();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
